#!/bin/bash

#------------------------------------------------------------------------------
#Filename:	75337-nfs.sh
#Version:	1.1
#Date:		2013/11/18
#Author:	yuanhui.shi
#Email:		yuanhui.shi@cs2c.com.cn
#Summary:	02.04网络服务-05NFS-01字符模式-02添加共享目录-05添加主机为指定域下所有主机
#Notes:         NFS配置——添加主机为指定域中所有主机
#Copyright:	China Standard Software Co.,Ltd.
#History:
#		Version 1.0,2013/11/20
#		-The first one
#-----------------------------------------------------------------------------


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../../lib/Echo.sh
source ./lib/Ssh.sh
source ../../../lib/XmlParse.sh
source ./lib/cmd.sh

function CleanData ()
{
  umount /mnt/rw &> /dev/null
  umount /mnt/ro &> /dev/null
  EchoInfo "关闭服务器端的nfs服务，删除共享目录"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "command -v systemctl && /bin/systemctl stop nfs.service || /sbin/service nfs stop"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "rm -rf /nfs-test"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo '' >/etc/exports"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "umount /mnt/rw"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "umount /mnt/ro"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "rm -rf /mnt/*"
  rm -rf  /mnt/*
  EchoInfo "75337-nfs.sh执行完毕"
}

trap "CleanData" EXIT INT
if [ -z "$SFROOT" ]
then
    echo "SFROOT is null"
    exit 1
fi

DATA_DIR="${SFROOT}/testcases/Network_Service"
DATA_FILE="config.xml"

XmlParse ${DATA_DIR}/${DATA_FILE}

GetValue ServerIP ReHost
GetValue USER UserName
GetValue PASSWORD RootPasswd

AuthDomainS="*.`hostname -d`none"

#DATA_DIR="./data"
#DATA_FILE="75337-nfs.xml"
#
#XmlParse ${DATA_DIR}/${DATA_FILE}
#
#GetValue USER USER
#GetValue ServerIP ServerIP
#GetValue PASSWORD PASSWORD
#GetValue AuthUSER AuthUSER
#GetValue AuthIP AuthIP
#GetValue AuthPASSWORD AuthPASSWORD
#GetValue AuthDomain  AuthDomain
#GetValue AuthDomainS  AuthDomainS

#远程配置服务器
EchoInfo "远程配置服务器${ServerIP}"

Scp "${USER}" "${ServerIP}" "${PASSWORD}" "./nfs-config.sh" "/root/"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sed -i s/*/${AuthDomainS}/g /root/nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sh nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo 查看服务器端nfs共享目录清单"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "showmount -e 127.0.0.1"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "rm -rf /root/nfs-config.sh"

#客户端是否可以查看到nfs共享目录
EchoInfo "客户端是否可以看见nfs共享目录"
stop_daemon iptables  

test -n "`showmount -e ${ServerIP} |grep /nfs-test/readonly`" -a -n "`showmount -e ${ServerIP} |grep /nfs-test/rw`" 
EchoResult "客户端可以看见nfs共享目录"

#---------------------------------授权客户端------------------------------------
#判断是否为授权客户端
#EchoInfo "判断客户端是否为授权客户端"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "test "`hostname`" == "${AuthDomain}""
#if [ $? -eq 0 ]
#then
#     Var1=`echo ${AuthDomain} |cut -d . -f 2,3`
#else 
#     exit 1
#fi
#Var2=`echo ${AuthDomainS} |cut -d . -f 2,3`
#
#if [ "${Var1}" != "${Var2}" ]
#then 
#    echo "请设定一个授权网段的客户端"
#    exit 1
#fi
#
##授权客户端在/mnt目录下新建两个目录：rw和ro
#EchoInfo "授权客户端新建目录"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mkdir /mnt/ro"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mkdir /mnt/rw"
#
#
##授权客户端挂载只读目录/nfs-test/readonly
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mount ${ServerIP}:/nfs-test/readonly /mnt/ro"
#EchoResult "授权客户端可以挂载只读目录" 
#
##授权客户端挂载读写目录/nfs-test/rw
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mount ${ServerIP}:/nfs-test/rw /mnt/rw"
#EchoResult "授权客户端可以挂载读写目录"

#--------------------------------非授权客户端-----------------------------------
#判断本地客户端属性
LocalDomain=`hostname -d`
Var3=`echo ${AuthDomainS} |cut -d . -f 2,3`


if [ "${LocalDomain}" = "${Var3}" ]
then 
    echo "请设定一个非授权网段的客户端"
    exit 1
fi

mount | grep ${ServerIP}:/nfs-test/readonly | grep /mnt/ro
[ $? -eq 0 ] && umount -f /mnt/ro
mount | grep ${ServerIP}:/nfs-test/rw | grep /mnt/rw
[ $? -eq 0 ] && umount -f /mnt/rw

#非授权客户端在/mnt目录下新建两个目录：rw和ro
[ -d /mnt/ro ] || mkdir /mnt/ro
[ -d /mnt/rw ] || mkdir /mnt/rw

#非授权客户端挂载只读目录/nfs-test/readonly
mount ${ServerIP}:/nfs-test/readonly /mnt/ro &>/dev/null

if [ $? -ne 0 ];then
    echo "PASS|非授权客户端不能挂载只读目录"
else
    echo "FAIL|非授权客户端不能挂载只读目录"
fi

#非授权客户端挂载读写目录/nfs-test/rw
mount ${ServerIP}:/nfs-test/rw /mnt/rw &>/dev/null

if [ $? -ne 0 ];then
    echo "PASS|非授权客户端不能挂载读写目录"
else
    echo "FAIL|非授权客户端不能挂载读写目录"
fi
